#include "dev/dacai/memory/memory.h"
#include "dev/dacai/driver/protocol.h"
#include "stdio.h"
#include "string.h"

void mem_setscreen(uint32 screen)
{
   SetScreen(screen);
}
struct t_contrilmsg *memgetcontrolid(struct t_contrilmsg * buf,uint32 len,uint32 screenid,uint32 controlid)
{
   uint32 i ;

   struct t_contrilmsg *p;

   for(i= 0;i<len;i++)
   {
      p = buf+i;
      if(p->screenid == screenid &&
         p->controlid == controlid)
      {
          return p;
      }
   }
   return 0;
}

struct t_screendeal *memgetdeal(struct t_contrilmsg * buf,uint32 len,uint32 screenid)
{
    uint32 i = 0;
    struct t_contrilmsg *p;

    for(i = 0;i<len;i++)
    {
       p = buf+i;
       if(p->screenid == screenid &&
          p->deal)
       {
           return (struct t_screendeal *)p->deal;
       }
    }
    return 0;
}

void mem_showcontrol(struct t_contrilmsg *control,uint8 state)
{
    if(!control)
    {
       return ;
    }
    SetControlVisiable(control->screenid,control->controlid,state);
    control->hide = !state;
}
void mem_controlenable(struct t_contrilmsg *control,uint8 enable)
{
    if(!control)
    {
       return ;
    }
   SetControlEnable(control->screenid, control->controlid,enable);
}
void men_seticonvalue( struct t_contrilmsg *icon,uint32 value)
{
   if(!icon)
   {
      return ;
   }
   if(icon->hide)
   {
       return ;
   }
   AnimationPlayFrame(icon->screenid,icon->controlid,value);
}

void button_trigger( struct t_contrilmsg *button,uint8 value)
{
    if(!button)
    {
      return ;
    }
    if(button->hide)
    {
        return ;
    }
    if(!value && !button->value)
    {
      button->value = 1;
    }
}
uint8 isbutton_trigger( struct t_contrilmsg *button)
{
    if(!button)
    {
      return 0;
    }
    if(button->hide)
    {
        return 0;
    }
    if(button->value)
    {
        button->value = 0;
        return 1;
    }
    return 0;
}

void beginrtc(struct t_contrilmsg *rtc,uint32 value)
{
   if(!rtc)
   {
      return ;
   }
   if(rtc->hide)
   {
     return ;
   }
   if(rtc->value)
   {
       return ;
   }
   StopTimer(rtc->screenid,rtc->controlid);
   SeTimer(rtc->screenid,rtc->controlid,value);
   StartTimer(rtc->screenid,rtc->controlid);
}
void rtctimeout(struct t_contrilmsg *rtc)
{
    if(!rtc)
    {
      return ;
    }
    if(rtc->hide)
    {
        return ;
    }
    if(!rtc->value)
    {
       rtc->value = 1;
    }
}
uint8 isrtctimeout(struct t_contrilmsg *rtc)
{
    if(!rtc)
    {
       return 0;
    }
    if(rtc->hide)
    {
        return 0;
    }
    if(rtc->value)
    {
        rtc->value = 0;
        return 1;
    }
    return 0;
}
void setrtc(int year,int month,int date,int hour,int min,int sec,int week)
{
	 SetRTC(year,month,date,hour,min,sec,week);
}
void readrtc(void)
{
	 ReadRTC();
}

void mem_settextvalue(struct t_contrilmsg *text,char *str)
{
    if(!text)
    {
       return ;
    }
    if(text->hide)
    {
       return ;
    }
    if(text->str)
    {
      sprintf(text->str,"%s",str);
    }
    SetTextValue(text->screenid,text->controlid,(unsigned char *)str);
}

void driver_settextvalue(struct t_contrilmsg *text,char *str)
{
    if(!text)
    {
       return ;
    }
    if(text->hide ||
       !text->str )
    {
      return ;
    }
    sprintf(text->str,"%s",str);
    text->value = 1;
}
uint8 driver_gettextvalue(struct t_contrilmsg *text,char *str)
{
    if(!text)
    {
       return 0;
    }
    if(text->hide ||
      !text->str  ||
      !text->value)
    {
        return 0;
    }
   
    sprintf(str,"%s",text->str);

    return 1;
}
uint8 driver_texttrigger(struct t_contrilmsg *text)
{
	 if(!text)
   {
     return 0;
   }
   if(text->hide ||
     !text->value)
   {
        return 0;
   }
	 text->value = 0;
	 return 1;
}	

uint8 mem_gettextvalue(struct t_contrilmsg *text,char *str)
{
    if(!text)
    {
       return 0;
    }

    if(text->hide ||
       !text->str)
    {
      return 0;
    }
    sprintf(str,"%s",text->str);
    return 1;
}
void mem_cleartext(struct t_contrilmsg *text)
{
    if(!text)
    {
       return ;
    }

    if(text->hide)
    {
        return ;
    }
    ClearTextValue(text->screenid, text->controlid);
}
void mem_sendgettextvalue(struct t_contrilmsg *text)
{
    if(!text)
    {
       return ;
    }
    if(text->hide)
    {
        return ;
    }
    GetControlValue(text->screenid,text->controlid);
}

void mem_setprogressvalue(struct t_contrilmsg *progress, uint8 value)
{
   if(!progress)
   {
       return ;
   }

   if(progress->hide)
   {
        return ;
   }
   SetProgressValue(progress->screenid,progress->controlid,value);
}

void mem_setqrcodevalue(struct t_contrilmsg *code,uint8 *str)
{
   if(!code)
   {
       return ;
   }
   if(code->hide)
   {
     return ;
   }
   SetTextValue(code->screenid,code->controlid,str);
}

